package com.carefree.system.modules.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.carefree.common.core.Result;
import com.carefree.common.emus.BusinessTypeEnum;
import com.carefree.common.emus.OperatorTypeEnum;
import com.carefree.common.valid.AddGroup;
import com.carefree.common.valid.UpdateGroup;
import com.carefree.system.common.annotation.SystemLog;
import com.carefree.system.modules.admin.entity.DeptEntity;
import com.carefree.system.modules.admin.service.DeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;


/**
 * 组织机构
 *
 * @author tengbin
 */
@Api(tags = "部门管理")
@RestController
@RequestMapping("/sys/dept")
public class DeptController {

    @Resource
    private DeptService deptService;


    /**
     * 列表
     *
     * @param params 分页参数
     * @return
     */
    @SystemLog(value = "查询部门信息并以树形结构显示", businessType = BusinessTypeEnum.SELECT, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "查询部门信息并以树形结构显示")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('btn:dept:list')")
    public Result<List<DeptEntity>> list() {
        List<DeptEntity> depts = deptService.getDeptWithTree();
        return Result.success(depts);
    }


    /**
     * 为用户管理模块查询不是一级(顶层)部门的部门信息
     *
     * @return
     */
    @SystemLog(value = "为用户管理模块查询不是级/顶层部门的部门信息", businessType = BusinessTypeEnum.SELECT, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "为用户管理模块查询不是一级/顶层部门的部门信息")
    @GetMapping("/list/notparent")
    @PreAuthorize("hasAuthority('btn:dept:list')")
    public Result<List<DeptEntity>> listForUserEdit() {
        List<DeptEntity> depts = deptService.list(new QueryWrapper<DeptEntity>().notIn("parent_id", 0));
        return Result.success(depts);
    }

    /**
     * 查询部门信息并添加顶层部门选项
     *
     * @return
     */
    @ApiOperation(value = "查询部门信息并添加顶层部门选项")
    @GetMapping("/list/edit")
    @PreAuthorize("hasAuthority('btn:dept:list')")
    public Result<List<DeptEntity>> listForEdit() {
        // 定义添加部门为"一级部门的情况"
        DeptEntity dept = new DeptEntity();
        dept.setId(0L);
        dept.setName("一级部门");
        dept.setParentId(0L);
        dept.setStatus(1);
        dept.setIsDeleted(0);
        // 查询所有部门信息
        List<DeptEntity> deptList = deptService.list();
        deptList.add(dept);

        return Result.success(deptList);
    }


    /**
     * 信息
     *
     * @param id
     * @return
     */
    @SystemLog(value = "根据部门id查询部门信息", businessType = BusinessTypeEnum.SELECT, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "根据部门id查询部门信息")
    @GetMapping("/info/{id}")
    @PreAuthorize("hasAuthority('btn:dept:list')")
    public Result<DeptEntity> info(@PathVariable("id") Long id) {
        DeptEntity dept = deptService.getById(id);
        return Result.success(dept);
    }

    /**
     * 保存
     *
     * @param Dept 组织机构
     * @return
     */
    @SystemLog(value = "保存部门信息", businessType = BusinessTypeEnum.INSERT, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "保存部门信息")
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('btn:dept:add')")
    public Result<Object> save(@Validated({AddGroup.class}) @RequestBody DeptEntity dept) {
        return deptService.saveDept(dept) ? Result.ok() : Result.error();
    }

    /**
     * 修改
     *
     * @param Dept 组织机构
     * @return
     */
    @SystemLog(value = "修改部门信息", businessType = BusinessTypeEnum.UPDATE, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "修改部门信息")
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('btn:dept:update')")
    public Result<Object> update(@Validated({UpdateGroup.class}) @RequestBody DeptEntity dept) {
        return deptService.updateDeptById(dept) ? Result.ok() : Result.error();
    }

    /**
     * 启用或禁用
     *
     * @param id
     * @return
     */
    @SystemLog(value = "根据id启用或禁用部门", businessType = BusinessTypeEnum.STATUS, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "根据id启用或禁用部门")
    @GetMapping("/active/{id}/{status}")
    @PreAuthorize("hasAuthority('btn:dept:enable')")
    public Result<Object> activeOrInActiveById(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        return deptService.activeOrInActiveById(id, status) ? Result.ok() : Result.error();
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @SystemLog(value = "根据id批量删除部门信息", businessType = BusinessTypeEnum.DELETE, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "根据id批量删除部门信息")
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('btn:dept:remove')")
    public Result<Object> delete(@RequestBody Long[] ids) {
        return deptService.removeByIds(Arrays.asList(ids)) ? Result.ok() : Result.error();
    }


}
